package com.wxw.test;



class MyData{
	
	
	
	byte[] mac;
	
	byte protocalVersion;
	int timeStamp;
}
	

public class Protocol {


 
	public static byte[] toByteArray(String hexString) {
		
	  if (hexString == null)
	  {
			return null;			
			
	  }
	  
	  //if (StringUtils.isEmpty(hexString))
	  {
			//return null;
			//throw new IllegalArgumentException("this hexString must not be empty");
			
	  }
	  
	 
		hexString = hexString.toLowerCase();
		final byte[] byteArray = new byte[hexString.length() / 2];
		int k = 0;
		for (int i = 0; i < byteArray.length; i++) {//因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
			byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
			byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
			byteArray[i] = (byte) (high << 4 | low);
			k += 2;
		}
		return byteArray;
	 }
 
    public static void main1(String[] args) {

		byte a = (byte)255;
		int b = ((byte)((a) & 0xf0))>>4;
		
		int c = ((byte)((a) & 0xf0))>>4;
		
 
		System.out.println("b = " + b);  
		System.out.println("c = " + c);  
 
        System.out.println("基本类型：float 二进制位数：" + Float.SIZE);  
        System.out.println("包装类：java.lang.Float");  
        System.out.println("最小值：Float.MIN_VALUE=" + Float.MIN_VALUE);  
        System.out.println("最大值：Float.MAX_VALUE=" + Float.MAX_VALUE);  
        System.out.println(); 


		String str = "FF550005";

		
		byte[] data = toByteArray(str);
		int val = (int)(data[0]>>4);
		System.out.println("val = "+val);
		
		System.out.println("data[0]"+data[0]);
		System.out.println("data[0]"+data[1]);
		System.out.println("data[0]"+data[2]);
		System.out.println("data[0]"+data[3]);
		
		System.out.printf("data[0] = %02x\n",data[0]);
		System.out.printf("data[0] = %02x\n",data[1]);
		System.out.printf("data[0] = %02x\n",data[2]);
		System.out.printf("data[0] = %02x\n",data[3]);

		data[0] = (byte)0xfe;
		
		System.out.printf("data[0] == %02x\n",data[0]);
		
		byte[] sb = str.getBytes();
		
		System.out.println("len = "+sb.length);
		
 		System.out.println("sb[0] = "+sb[0]);
		System.out.println("sb[0] = "+sb[1]);
		System.out.println("sb[0] = "+sb[2]);
		System.out.println("sb[0] = "+sb[3]);  
		System.out.println("len ==================== "+sb.length);
		
		System.out.printf("sb[0] = %x\n",sb[0]);
		System.out.printf("sb[0] = %x\n",sb[1]);
		System.out.printf("sb[0] = %x\n",sb[2]);
		System.out.printf("sb[0] = %x\n",sb[3]);
		System.out.printf("sb[0] = %x\n",sb[4]);
		System.out.printf("sb[0] = %x\n",sb[5]);
		System.out.printf("sb[0] = %x\n",sb[6]);
		System.out.printf("sb[0] = %x\n",sb[7]);
		System.out.printf("sb[0] = %s\n",sb);
		
		//618B1FE9
		
		
		byte[] protocolData = {(byte)0x61,(byte)0x8B,(byte)0x1F,(byte)0xE9,(byte)0xff,(byte)0xfe,(byte)1,(byte)2,3,4,5,6,7,8};


		for (int i = 0;i < protocolData.length;i++)
		{
			System.out.printf("protocolData[i]  = 0x%02x\n",protocolData[i]);
		}
		

		
		//直接解码
		//(1) ok 
		//int temp =  ((int)((protocolData[0]<<8)&0xff00));		
		//temp |= (int)(protocolData[1] & 0xff);

		//(2)ok
		//int temp =  ((int)((protocolData[0]<<8)&0xff00)) | (int)(protocolData[1] & 0xff);	
		
		//(3)ok
		//int temp = ((protocolData[0]<<8)&0xff00) | (protocolData[1] & 0xff);

		//(4)ok
		//int temp = ((protocolData[0]<<24)&0xff000000) | ((protocolData[1]<<16)&0xff0000) |((protocolData[2]<<8)&0xff00) |((protocolData[3]<<0)&0xff);
		
		//(5)
		//int temp = (protocolData[0] <<< 8) | (protocolData[1] & 0xff);		
		
		int temp = ((protocolData[0]<<24)&0xff000000);
		temp += ((protocolData[1]<<16)&0xff0000);
		temp +=((protocolData[2]<<8)&0xff00);
		temp +=((protocolData[3]<<0)&0xff);
		
 
		
		int total = ((protocolData[4]<<8)&0xff00);
		temp +=((protocolData[5]<<0)&0xff);
		
		
		MyData mydata = new MyData();
		
		//System.out.printf("protocolData[0] = %b\n",protocolData[0]);
		//System.out.printf("protocolData[0] = %b\n",protocolData[1]);
		System.out.printf("protocolData[0] = %d\n",temp);
		System.out.printf("protocolData[0] = 0x%x\n",temp);
		
		mydata.timeStamp = temp;
		mydata.protocalVersion = protocolData[2];
		System.out.printf("mydata.timeStamp = 0x%x\n",mydata.timeStamp);
		System.out.printf("mydata.protocalVersion = 0x%x\n",mydata.protocalVersion);
		
		//转字符串解码

		String myDataStr = new String(protocolData);
		
		System.out.println(myDataStr);
		//System.out.printf("myDataStr = 0x%s\n",myDataStr);		
		System.out.println("DataStr = "+myDataStr);
		
	
		mydata.mac=new byte[6];
		
		for (int i = 0;i < 6;i++)
		{
			mydata.mac[i] = protocolData[i];
			//System.out.println(mydata.mac[i]);
			System.out.printf("mydata.mac[i]  = 0x%02x\n",mydata.mac[i] );
		}
		
		System.out.println(myDataStr.toString());
		//System.out.println(IntegerToHexSting(myDataStr));
    }  
}

